<template>
  <section class="welfare-recharge-main">
     <section class="main-search merge">
      <span class="svg-container">
        <svg-icon icon-class="search" />
      </span>
      <el-input v-model.trim="listQuery.name" placeholder="输入充值名称查询" class="search-input" @keyup.enter.native="handleFilter" />
      <section class="search-btn">
        <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
        <el-button type="primary" @click="handleReset">重置</el-button>
      </section>
    </section>

    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>充值记录</span>
      </section>
      <section class="main-buttons-btns">
        <el-button style="margin-left: 10px" @click="handleCreate">新建</el-button>
      </section>
    </section>
    <section class="main-table">
      <v-table :list="list" tag="rechargeRecord" :list-loading="listLoading" :filter-column="filtercolumn">
        <el-table-column label="操作" fixed="right" align="center" min-width="100" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="toDetail(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </v-table>
       <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </section>
      <!--新建编辑-->
    <el-dialog v-el-drag-dialog :destroy-on-close="true" :visible.sync="dialogVisible" :close-on-click-modal="false"
               title="新建" center width="800px">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="名称" prop="applyName">
          <el-input style="width: 100%;" v-model="form.applyName" label="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="账号" prop="accountId">
          <el-select style="width: 100%;" v-model="form.accountId" placeholder="请选择">
            <el-option v-for="item in accountList" :key="item.dictValue" :label="getName(item)" :value="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="用途" prop="purpose">
          <el-select style="width: 100%;" v-model="form.purpose" placeholder="请选择">
            <el-option v-for="item in dict.PURPOSE" :key="item.dictValue" :label="item.dictName"
                       :value="item.dictValue"/>
          </el-select>
        </el-form-item>
        <el-form-item label="充值金额（元）" prop="rechargeAmount" class="recharge-money">
          <!--<el-input
                 @input="value=value.replace(/^\.+|[^\d.]/g,'')"
                 @blur="rechBlur"
                 v-model="form.rechargeAmount"
                 placeholder="请输入充值金额"
             ></el-input>-->
          <el-input-number style="width: 100%;" v-model="form.rechargeAmount" :precision="2" :controls="false"
                           label="请输入充值金额"></el-input-number>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" :rows="6" type="textarea" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="default" style="padding: 10px 30px" @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="createSubmit('form')">确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { queryPage, getDictByCode,submit,queryEnter} from '../api'
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import elDragDialog from '@/directive/el-drag-dialog'
import CryptoJS from '@/utils/aesJiaJM'
export default {
  name: 'Recharge',
  components: { VTable, Pagination },
  directives: {elDragDialog},
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        name: ''
      },
      dialogVisible:false,
      list: [],
      total: 0,
      listLoading: false,
      accountList: [],
    	dict: {
        PURPOSE: [] // 账户类型
      },
      form: {
        applyName: '',
        rechargeAmount: '',
        remark: '',
        purpose: ''
      },
      rules: {
        applyName: [{required: true, message: '请输入名称', trigger: 'blur'}],
        purpose: [{required: true, message: '请选择用途', trigger: 'change'}],
        rechargeAmount: [{required: true, message: '请输入充值金额', trigger: 'blur'}]
      },

    }
  },
  created() {
    this.getAccount()
    for (const key in this.dict) {
      this.getDict(key)
    }
    this.getList()
  },
  methods: {
    getName(ite) {
      return ite.name + '（' + CryptoJS.$aesJieM(ite.accountNumber) + '）'
    },
    toDetail(row){
      this.$router.push({path: this.$filterPath('info'),query:{i:row.id}})
    },
		getDict(code) {
			return new Promise((resolve, reject) => {
				getDictByCode(code).then(response => {
					this.dict[code] = response.data.data[0].list
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		},
    getAccount() {
      queryEnter({accountStatus: 'ZHZT01'}).then(response => {
        this.accountList = response.data.data || []
      }).catch(error => {

      })
    },
    filtercolumn(scope, item) {
      if (item === 'rechargeNumber') {
        return CryptoJS.$aesJieM(scope.row[item])
      } else {
        return scope.row[item]
      }
    },
    handleReset() {
      this.listQuery.page = 1
      this.listQuery.name = ''
      this.getList()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    handleCreate() {
      for (const key in this.form) {
        this.form[key] = ''
      }
      this.type = 'create'
      this.dialogVisible = true
    },
    createSubmit(formName) {
      if (this.form.rechargeAmount <= 0 ) {
        this.$message({
          message: '充值金额必须大于0！',
          type: 'warning'
        })
        return
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          const params = Object.assign({}, this.form)
          return new Promise((resolve, reject) => {
            submit(params, this.type).then(() => {
              loading.close()
              this.$message({
                title: '成功',
                message: '操作成功',
                type: 'success',
                duration: 2000
              })
              this.getList()
              this.dialogVisible = false
            }).catch(error => {
              loading.close()
              reject(error)
            })
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },

    getList(obj) {
			this.listLoading = true
			var query = {
				page: obj ? obj.page : this.listQuery.page,
				pageSize: obj ? obj.limit : this.listQuery.limit,
        applyName: this.listQuery.name
			}
      return new Promise((resolve, reject) => {
        queryPage(query).then(response => {
          this.list = response.data.data.rows || []
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    }
  }
}
</script>
<style lang="scss">
@import '@/styles/mixin.scss';
.welfare-recharge-main {
  padding: 10px;
  .recharge-money input {
    text-align: left;
  }
  .main-search {
    background-color: #fff;
    padding: 0 10px;
    .svg-container {
      padding: 0 5px 0 10px;
      color: #bfced6;
      vertical-align: middle;
      display: inline-block;
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }
  }
  .classify-tab {
    width: 100%;
    margin-bottom: 10px;
    padding: 0 10px;
    background-color: #fff;
    display: flex;
    border-radius: 4px;
    .el-tabs__nav-wrap::after {
      display: none;
    }
    .el-tabs__header {
      margin: 0;
    }
  }
  .main-buttons {
    display: flex;
    line-height: 36px;
    margin-bottom: 10px;
    .main-buttons-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
      .num {
        @include font-color($mainColor);
      }
    }
  }
}
</style>
